如何JSON解析Android中JSON、Android

由网友(该走的走该留的留)分享简介:http://docs.blackberry.com/sampledata.json 这是我的web服务,我想分析和检索vehicleType,vehicleColor,燃料,姓名,experiencePoints,treadType。This is my web service and I want to...

http://docs.blackberry.com/sampledata.json

这是我的web服务,我想分析和检索vehicleType,vehicleColor,燃料,姓名,experiencePoints,treadType。

This is my web service and I want to parse and retrieve vehicleType, vehicleColor, fuel, name, experiencePoints, treadType.

public class MainActivity extends Activity {

    private static String url="http://maps.googleapis.com/maps/api/directions/json?origin=Tamil%20ndau,Salem&destination=Tamil%20nadu,%20salem&region=in&sensor=false";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        JSONObject json=JSONParser.getJSONfromURL(url);  // here i am able to read content of that web service 
    }
} catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

请告诉我,我怎么能得到的价值?我不能来接节点JSON对象和JSON数组。请告诉我,我必须做下一个,这样我可以检索值。我的JSON解析和Android是新的。请帮我。

Please tell me how can I get the value? I'm not able to pick node for Json object and Json array. Please tell me what I have to do next so that I can retrieve the value. I am new in Json parsing and android. Please help me.

推荐答案

可能对你这个code有助于

May be this code help for you

public class JSONfunctions {

public static JSONArray getJSONfromURL(String url) {
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to string
    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();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag get data string ",
                "Error converting result " + e.toString());
    }

    try {

        jArray = new JSONArray(result);
    } catch (JSONException e) {
        Log.e("log_tag create object ",
                "Error parsing data " + e.toString());
    }

    return jArray;
}
  }

这是解析code例如可以改变这一点。

This is parsing code example you can change this.

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    JSONArray json = JSONfunctions
            .getJSONfromURL("http://docs.blackberry.com/sampledata.json");

    for (int i = 0; i < json.length(); i++) {

        try {
            Log.e("json names vehicleType", ""
                    + json.getJSONObject(i).getString("vehicleType"));
            Log.e("json names vehicleColor", ""
                    + json.getJSONObject(i).getString("vehicleColor"));
            Log.e("json names fuel",
                    "" + json.getJSONObject(i).getString("fuel"));
            Log.e("json names treadType", ""
                    + json.getJSONObject(i).getString("treadType"));

            Log.e("json names approvedOperators",
                    ""
                            + json.getJSONObject(i).getJSONArray(
                                    "approvedOperators"));

            JSONArray array = json.getJSONObject(i).getJSONArray(
                    "approvedOperators");
            for (int j = 0; j < array.length(); j++) {

                Log.e("json names approvedOperators name ", ""
                        + array.getJSONObject(j).getString("name"));
                Log.e("json names approvedOperators experience ",
                        ""
                                + array.getJSONObject(j).getString(
                                        "experiencePoints"));

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
}
阅读全文

相关推荐

最新文章