Android JSON API parsing

2019-08-19 23:29发布

I have tried several tutorials using JSON parsing but I can't seem to make them work. 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");
    }
}

2条回答
▲ chillily
2楼-- · 2019-08-20 00:18

Json object may contain a number of key value pairs. In the jString posted in your question, strings like "searchTerm", "searchableLocations", etc are the String keys. The values could be anything like String, Int, Bollean, JSonArray, etc. Corresponding to the key "searchableLocations" the value is a JsonArray (denoted by [ ]).

    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");
查看更多
Fickle 薄情
3楼-- · 2019-08-20 00:32

Does this code solve your problem?

Gson gson = new Gson();
MyClass obj = gson.fromJson(jString ,MyClass.class);
查看更多
登录 后发表回答