How to get the value form json object inside form another json object where keys aye dynimic

StackOverflow https://stackoverflow.com/questions/22654423

  •  21-06-2023
  •  | 
  •  

سؤال

I want to get the value from below json response. I am able to get the value from first key but not anther key object. I want to get the value of all url but no of url is dynamic and key too. Below is json response:

I am able to get the value of body, title and created_date

{
"17": {
    "27": {
        "url": "some text here 1"
    },
    "28": {
        "url": "some text here 12"
    },
    "29": {
        "url": "some text here 123"
    },
    "title": "some text goes here",
    "body": "Some text goes here",
    "created_date": "1395386881"
 }
}

Code what i am trying is below:

try {
        json = new JSONTokener(jsonNewsResponse).nextValue();
        if (json instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) json;
            Iterator<?> keys = jsonObject.keys();
            while (keys.hasNext()) {
                nid = String.valueOf(keys.next());
                JSONObject jsonObj = jsonObject.getJSONObject(nid);
                attributeValue = jsonObj.getString(attributeName);
                    List<String> listitems = new ArrayList<String>();
                    Iterator<?> key = jsonObj.keys();
                    while (key.hasNext()) {

                        String fid = String.valueOf(key.next());
                        System.out.println("FID"+fid);
                        System.out.println("FID"+fid);
                        System.out.println("FID"+fid);
                        System.out.println("FID"+fid);
                        JSONObject jObject = jsonObj.getJSONObject(fid);
                        listitems.add(jObject.getString("url"));
                    }
                    attachments = listitems.toArray(new String[0]);
            }
        }
هل كانت مفيدة؟

المحلول

Try this..

    JSONObject Jobj;
    try {
        Jobj = new JSONTokener(jsonNewsResponse).nextValue();
        if (Jobj instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) Jobj;
            Iterator<?> keys = jsonObject.keys();
            while (keys.hasNext()) {
                String nid = String.valueOf(keys.next());
                JSONObject jsonObj = jsonObject.getJSONObject(nid);
                    Iterator<?> key = jsonObj.keys();
                    while (key.hasNext()) {
                        String fid = String.valueOf(key.next());
                        Log.v("fid--",""+fid);

                        if( jsonObj.get(fid) instanceof JSONObject){
                            JSONObject jObject = jsonObj.getJSONObject(fid);
                            String url = jObject.getString("url");
                                    Log.v("url--",""+url);
                        }else{
                                String fidfadd = jsonObj.getString(fid);
                                Log.v("fidfadd--",""+fidfadd);
                            }
                     }
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

نصائح أخرى

Gson gson=new Gson();
Map<String,Object> myMap=gson.fromJson( "your_json_string_after_getting_first_body", Map.class );

map's key = your json object's key. And value is jsonObjects that contains keys and urls. And don't use your whole json into it. Just for below part.

"27": {
    "url": "some text here 1"
},
"28": {
    "url": "some text here 12"
},
"29": {
    "url": "some text here 123"
},

but of course you can extend it whatever you want. GSON

Use the Gson library.

YourObjectsClass yourObject = new Gson().fromJson(json, YourObjectsClass.class)

Now that I look at it more closely, I'm not sure that json can map to a very good object model in Java. You have fields with the key "27" on the same tier as fields with real String names, like "title", but they have different types of objects as their values (some have a string, some have a complex object that contains one field with the name "url"). Java field names cannot start with a number, though, so you can't make a normal Java class out of it, and you can't make a map out of it either, unless it is a map that accepts only <Object> as its value, which isn't ideal.

So I would recommend you fix your Json, then make a model class to represent it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top