0

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");
    }
}
5
  • 1
    Why doesn't it work? Are there any error messages? What is your expected and actual output? Commented Nov 21, 2011 at 11:02
  • for(inti=0i<searchableLocations.length() typo? Commented Nov 21, 2011 at 11:05
  • why doesn't work? what is your problem exactly, what you require exactly? Commented Nov 21, 2011 at 11:06
  • The message i'm recieving is Error parsing data org.jsonjJSONException:No value for searchableLocation – Mohamed 5 mins ago Commented Nov 21, 2011 at 11:29
  • Here is what i'm trying to retrieve {"searchTerm":"N7","searchableLocations":[{"identifier":"OUTCODE^1685","name":"N‌​7"}],"createDate":1321834118923,"result":"SUCCESS"} but i'm recieving nothing Commented Nov 21, 2011 at 11:30

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Kartik/ Amy,I think your solution will work,but at the moment i'm recieving erros.These erros are 11-21 10:40:39.687: D/AndroidRuntime(447): Shutting down VM 11-21 10:40:39.687: W/dalvikvm(447): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 11-21 10:40:39.826: E/AndroidRuntime(447): and at the end says,android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 11-21 12:47:49.946: E/AndroidRuntime(568): ... 11 more 11-21 12:47:55.286: I/Process(568): Sending signal. PID: 568 SIG: 9
Is there any specific line in the code were the error occurs? Does that happen only if the parse() method is called?
0

Does this code solve your problem?

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

1 Comment

I'm not sure how it will help,please could you explain to me how it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.