简体   繁体   English

Android JSON API解析

[英]Android JSON API parsing

I have tried several tutorials using JSON parsing but I can't seem to make them work. 我已经尝试过使用JSON解析的一些教程,但是似乎无法使它们正常工作。 Please can anyone kindly help and direct me to the right direction? 请任何人能帮助并指导我正确的方向吗?

I have now made some changes to my previous class with the help of a couple of helpful people, and I'm now getting less errors. 现在,在几个有帮助的人的帮助下,我对上一堂课进行了一些更改,现在我得到的错误更少了。 Please help. 请帮忙。

package com.somcollection;

import android.app.Activity;
import android.os.Bundle;

import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;

public class JsonParser extends Activity {
    private JSONObject jObject;
    private String jString = "{\"searchTerm\":\"N7\",\"searchableLocations\":[{\"identifier\":\"OUTCODE^1685\",\"name\":\"N7\"}],\"createDate\":1321834118923,\"result\":\"SUCCESS\"}";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            parse();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void parse() throws Exception {
        jObject = new JSONObject(jString);
        JSONObject jObject = new JSONObject(jString);
        String menuObject = jObject.getString("searchTerm");
        JSONArray menuitemArray = jObject.getJSONArray("searchableLocations");
        for (int i = 0; i < menuitemArray.length(); i++) {
            JSONObject jsonObj = menuitemArray.getJSONObject(i);
            String attributeId = jsonObj.getString("identifier");
            System.out.println(attributeId);
            String attributeValue = jsonObj.getString("name");
            System.out.println(attributeValue);
        }
        String createDate = jObject.getString("jObject");
        String result = jObject.getString("result");
    }
}

Json object may contain a number of key value pairs. Json对象可以包含许多键值对。 In the jString posted in your question, strings like "searchTerm", "searchableLocations", etc are the String keys. 在问题中发布的jString中,诸如“ searchTerm”,“ searchableLocations”等字符串是String键。 The values could be anything like String, Int, Bollean, JSonArray, etc. Corresponding to the key "searchableLocations" the value is a JsonArray (denoted by [ ]). 值可以是String,Int,Bollean,JSonArray等。与键“ searchableLocations”对应的值是JsonArray(用[]表示)。

    JSONObject jObject = new JSONObject(jString);    
    String menuObject = jObject.getString("searchTerm");     
    JSONArray menuitemArray = jObject.getJSONArray("searchableLocations");   

    for (int i = 0; i < menuitemArray.length(); i++) {
        JSONObject jsonObj = menuitemArray.getJSONObject(i);     
        String attributeId = jsonObj.getString("identifier");     
        System.out.println(attributeId);     

        String attributeValue = jsonObj.getString("name");    
        System.out.println(attributeValue); 
    }

    String createDate = jObject.getString("jObject");
    String result = jObject.getString("result");

Does this code solve your problem? 这段代码能解决您的问题吗?

Gson gson = new Gson();
MyClass obj = gson.fromJson(jString ,MyClass.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM