Java JSON数据解析Android Studio

Java JSON数据解析Android Studio,java,android,json,parsing,Java,Android,Json,Parsing,我目前是android编程的初学者,现在我正在开发一个应用程序来与服务器建立JSON连接并显示JSON数据。连接目前已成功建立,但我在应用程序上显示JSON数据时遇到问题。我的JSON数据的格式如下` 我第一次使用JSONObject而不是JSONArray,因为我意识到这些地方不正确,所以我认为我已经正确地更改了它们。但是在我的主要活动中,我现在对如何在我的应用程序上显示JSON连接的数据感到困惑。这里的任何帮助都将不胜感激。“我当前的错误”出现在所有三个节点所在的MainActivity中

我目前是android编程的初学者,现在我正在开发一个应用程序来与服务器建立JSON连接并显示JSON数据。连接目前已成功建立,但我在应用程序上显示JSON数据时遇到问题。我的JSON数据的格式如下`

我第一次使用JSONObject而不是JSONArray,因为我意识到这些地方不正确,所以我认为我已经正确地更改了它们。但是在我的主要活动中,我现在对如何在我的应用程序上显示JSON连接的数据感到困惑。这里的任何帮助都将不胜感激。“我当前的错误”出现在所有三个节点所在的MainActivity中

错误:(46,25)错误:类JSONArray中的方法getJSONArray不能应用于给定的类型; 必填项:int 找到:字符串 原因:无法通过方法调用转换将实际参数字符串转换为int

我还将在下面介绍我的课程:

主要活动类

    import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends ActionBarActivity {



    //URL to get JSON Array
    private static String url = "";

    //JSON Node Names
    private static final String Node_ID = "id";
    private static final String Node_BusinessName = "BusinessName";

    JSONArray array = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONArray json = jParser.getJSONFromUrl(url);
        try {
            // Getting JSON Array
            array = json.getJSONArray(Node_BusinessName);
            JSONArray c = array.getJSONArray(0);

            // Storing  JSON item in a Variable
            String id = c.getString(Node_ID);
            String businessName = c.getString(Node_BusinessName);


            //Importing TextView
            final TextView ID = (TextView)findViewById(R.id.ID);
            final TextView BusinessNameText = (TextView)findViewById(R.id.BusinessName);

            //Set JSON Data in TextView
            ID.setText(id);
            BusinessNameText.setText(businessName);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
JSONParser类

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;


public class JSONParser {

    static InputStream is = null;
    static JSONArray jObj = null;
    static String json = "";


    // constructor
    public JSONParser() {
    }
    public JSONArray getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;
    }
}

首先,我认为您的json文本已损坏。 如果问题在于转换数据,您可以将其作为字符串或对象获取,然后使用java代码进行转换。

您的问题就在这里

JSONArray json = jParser.getJSONFromUrl(url);
 try {
       array = json.getJSONArray(Node_BusinessName);
       JSONArray c = array.getJSONArray(0);
您的第一个标记是
JSONArray
,在
JSONParser
类中,您已经返回了
JSONArray
。所以只要去掉这条线

array = json.getJSONArray(Node_BusinessName);
和使用一样

for(int i=0; i<json.length();i++){
  JSONObject jb = json.getJSONObject(i);
  String id = jb.getString(Node_ID);
  // same for other keys
}
for(int i=0;i
for(int i=0;ivalue
地图放置(标签PID,id);
地图放置(标签名称、名称);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
试试这个

并尝试将此JSON代码放入更有效的异步任务中


请尝试遵循。您将了解更多信息。

请先格式化您的代码。
for (int i = 0; i < products.length(); i++) {
    JSONObject c = products.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(TAG_PID);
    String name = c.getString(TAG_NAME);

    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put(TAG_PID, id);
    map.put(TAG_NAME, name);

    // adding HashList to ArrayList
    productsList.add(map);
}