0

I'm new to JSON and is having trouble reading this one..

{
1: "Sarath"
2: "nagesh"
3: "R"
4: "06/29/1991"
5: "567-77-4576"
6: "(999) 540-8778"
7: "(655) 658-7369"
8: "(398) 967-3996"
9: "Male"
10: "sarathnagesh@gmail.com"
11: "89"
12: "21 Wallstreet"
13: "22331"
14: "Alexandria"
15: "VA"
16: "ASD"
17: "34"
18: "Brooklyn Avenue"
19: "22331"
20: "Alexandria"
21: "VA"
22: "A+"
23: "1"
24: "m"
25: "65"
26: "cm"
27: "56"
28: "kg"
29: "0"
30: "gm"
31: "Single"
32: ""
33: ""
34: ""
35: ""
36: ""
37: ""
38: ""
39: ""
53: "[{"54":"XYZ Fincance","55":"Reese","56":"Son","57":"Group1","58":"jhg76fv7","59":"Dsad","60":"Dfdsf","61":"73301","62":"Austin","63":"TX","64":"(634) 543-5345","101":"Yes","102":"Yes","103":"03/20/2015"},{"54":"ABC Insurance","55":"Ghv","56":"Son","57":"Group A","58":"2813981239821","59":"NH-501","60":"North Avenue","61":"22331","62":"Alexandria","63":"VA","64":"(742) 965-2838","101":"Yes","102":"Yes","103":"12/18/2034"}]"
150: ""
154: ""
159: ""
164: ""
165: ""
309: "[{"41":"Smith","42":"John","43":"S","44":"uncle","45":"(213) 123-2132","46":"(454) 324-5892","47":"(123) 456-7890","48":"Sa567","49":"AK","50":"11221","51":"Brooklyn","52":"NY"},{"41":"Smith2","42":"John","43":"2","44":"Uncle","45":"(723) 912-3912","46":"(217) 483-1912","47":"(475) 901-0371","48":"HY","49":"HU","50":"22331","51":"Alexandria","52":"VA","undefined":"Uncle"},{"41":"Harkness","42":"Jack","43":"Cap","44":"Son","45":"(989) 594-2019","46":"(917) 912-8129","47":"(371) 927-3912","48":"Thualsdj","49":"Nelujah","50":"22321","51":"Alexandria","52":"VA"}]"
310: "Smith"
311: "John"
313: "(111) 111-1111"
314: "johnsmith@gmail.com"
315: "sarathnagesh@live.com"
318: "ABCD"
320: "Dept1"
500: "[{"502":"Abc Pharnacy","504":"45-101","506":"South Avenue","508":"22331","510":"VA","512":"Alexandria","514":"(123) 213-2132","516":"(123) 213-2131"},{"502":"Gcd Pharmacy0","504":"45-10","506":"D-560","508":"22331","510":"VA","512":"Alexandria","514":"(731) 952-0520","516":"(841) 804-1930"}]"
1024: "["Do not Resuscitate","Healthcare Proxy"]"
1051: "Yes"
}

I'm trying to get the value of the strings named 41 and 42 from the array named 309. This is what I tried.

JSONObject jsonObject = new JSONObject(result);
String emergencyContactName = jsonObject.getJSONArray("309").getJSONObject(0).
                    getString("42") + " " + jsonObject.getJSONArray("309").getJSONObject(0).
                    getString("41");

Log:

04-20 10:52:41.041    9279-9279/com.cinch.gogch W/System.err﹕ org.json.JSONException: Value [{"41":"Smith","42":"John","43":"S","44":"uncle","45":"(213) 123-2132","46":"(454) 324-5892","47":"(123) 456-7890","48":"Sa567","49":"AK","50":"11221","51":"Brooklyn","52":"NY"},{"41":"Smith2","42":"John","43":"2","44":"Uncle","45":"(723) 912-3912","46":"(217) 483-1912","47":"(475) 901-0371","48":"HY","49":"HU","50":"22331","51":"Alexandria","52":"VA","undefined":"Uncle"},{"41":"Harkness","42":"Jack","43":"Cap","44":"Son","45":"(989) 594-2019","46":"(917) 912-8129","47":"(371) 927-3912","48":"Thualsdj","49":"Nelujah","50":"22321","51":"Alexandria","52":"VA"}] at 309 of type java.lang.String cannot be converted to JSONArray
04-20 10:52:41.041    9279-9279/com.cinch.gogch W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
04-20 10:52:41.041    9279-9279/com.cinch.gogch W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:588)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at com.cinch.gogch.tasks.EmergencyCardTask.onPostExecute(EmergencyCardTask.java:94)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at com.cinch.gogch.tasks.EmergencyCardTask.onPostExecute(EmergencyCardTask.java:36)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-20 10:52:41.042    9279-9279/com.cinch.gogch W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

this is how normally read from JSON array but its not working with this one. Can someone be kind enough to help me?

EDIT

{"35":"","36":"","159":"","33":"","34":"","39":"","37":"","154":"","38":"","1051":"Yes","150":"","22":"A+","23":"1","24":"m","25":"65","26":"cm","165":"","309":"[{\"41\":\"Smith\",\"42\":\"John\",\"43\":\"S\",\"44\":\"uncle\",\"45\":\"(213) 123-2132\",\"46\":\"(454) 324-5892\",\"47\":\"(123) 456-7890\",\"48\":\"Sa567\",\"49\":\"AK\",\"50\":\"11221\",\"51\":\"Brooklyn\",\"52\":\"NY\"},{\"41\":\"Smith2\",\"42\":\"John\",\"43\":\"2\",\"44\":\"Uncle\",\"45\":\"(723) 912-3912\",\"46\":\"(217) 483-1912\",\"47\":\"(475) 901-0371\",\"48\":\"HY\",\"49\":\"HU\",\"50\":\"22331\",\"51\":\"Alexandria\",\"52\":\"VA\",\"undefined\":\"Uncle\"},{\"41\":\"Harkness\",\"42\":\"Jack\",\"43\":\"Cap\",\"44\":\"Son\",\"45\":\"(989) 594-2019\",\"46\":\"(917) 912-8129\",\"47\":\"(371) 927-3912\",\"48\":\"Thualsdj\",\"49\":\"Nelujah\",\"50\":\"22321\",\"51\":\"Alexandria\",\"52\":\"VA\"}]","27":"56","28":"kg","500":"[{\"502\":\"Abc Pharnacy\",\"504\":\"45-101\",\"506\":\"South Avenue\",\"508\":\"22331\",\"510\":\"VA\",\"512\":\"Alexandria\",\"514\":\"(123) 213-2132\",\"516\":\"(123) 213-2131\"},{\"502\":\"Gcd Pharmacy0\",\"504\":\"45-10\",\"506\":\"D-560\",\"508\":\"22331\",\"510\":\"VA\",\"512\":\"Alexandria\",\"514\":\"(731) 952-0520\",\"516\":\"(841) 804-1930\"}]","29":"0","3":"R","2":"nagesh","1":"Sarath","164":"","7":"(655) 658-7369","30":"gm","6":"(999) 540-8778","5":"567-77-4576","32":"","4":"06/29/1991","31":"Single","9":"Male","8":"(398) 967-3996","318":"ABCD","19":"22331","314":"johnsmith@gmail.com","17":"34","315":"sarathnagesh@live.com","18":"Brooklyn Avenue","15":"VA","313":"(111) 111-1111","16":"ASD","310":"Smith","13":"22331","311":"John","14":"Alexandria","11":"89","1024":"[\"Do not Resuscitate\",\"Healthcare Proxy\"]","12":"21 Wallstreet","21":"VA","20":"Alexandria","320":"Dept1","10":"sarathnagesh@gmail.com","53":"[{\"54\":\"XYZ Fincance\",\"55\":\"Reese\",\"56\":\"Son\",\"57\":\"Group1\",\"58\":\"jhg76fv7\",\"59\":\"Dsad\",\"60\":\"Dfdsf\",\"61\":\"73301\",\"62\":\"Austin\",\"63\":\"TX\",\"64\":\"(634) 543-5345\",\"101\":\"Yes\",\"102\":\"Yes\",\"103\":\"03/20/2015\"},{\"54\":\"ABC Insurance\",\"55\":\"Ghv\",\"56\":\"Son\",\"57\":\"Group A\",\"58\":\"2813981239821\",\"59\":\"NH-501\",\"60\":\"North Avenue\",\"61\":\"22331\",\"62\":\"Alexandria\",\"63\":\"VA\",\"64\":\"(742) 965-2838\",\"101\":\"Yes\",\"102\":\"Yes\",\"103\":\"12/18/2034\"}]"}
Drunken Daddy
  • 7,326
  • 14
  • 70
  • 104

3 Answers3

2

First of all, you have provided wrong input format. I have made some assumptions and generate basic input for code to run.

I have done some basic code, from that you will get an idea that how to and what to do ?

Here is a basic sample code, I have used some sample String as JSON:

public static void main(String args[]) throws JSONException {
        String json = "{" +
                "  \"1\": \"\"," +
                "  \"2\": \"\"," +
                "  \"309\": [" +
                "      {" +
                "        \"41\": \"John\"," +
                "        \"42\": \"Smith\"" +
                "      }" +
                "    ]" +
                "}";
        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("309");
        for(Integer i = 0 ; i < jsonArray.length() ; i++){
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if(jsonObject1.has("41")){
                System.out.println(jsonObject1.get("41"));
            }
            if(jsonObject1.has("42")){
                System.out.println(jsonObject1.get("42"));
            }
        }
}

Let me know, if you have some problem.

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
1

Change your json:

{
"1": "Sarath",
"2": "nagesh",
"3": "R",
"4": "06/29/1991",
"5": "567-77-4576",
"6": "(999) 540-8778",
"7": "(655) 658-7369",
"8": "(398) 967-3996",
"9": "Male",
"10": "sarathnagesh@gmail.com",
"11": "89",
"12": "21 Wallstreet",
"13": "22331",
"14": "Alexandria",
"15": "VA",
"16": "ASD",
"17": "34",
"18": "Brooklyn Avenue",
"19": "22331",
"20": "Alexandria",
"21": "VA",
"22": "A+",
"23": "1",
"24": "m",
"25": "65",
"26": "cm",
"27": "56",
"28": "kg",
"29": "0",
"30": "gm",
"31": "Single",
"32": "",
"33": "",
"34": "",
"35": "",
"36": "",
"37": "",
"38": "",
"39": "",
"53": [{"54":"XYZ Fincance","55":"Reese","56":"Son","57":"Group1","58":"jhg76fv7","59":"Dsad","60":"Dfdsf","61":"73301","62":"Austin","63":"TX","64":"(634) 543-5345","101":"Yes","102":"Yes","103":"03/20/2015"},{"54":"ABC Insurance","55":"Ghv","56":"Son","57":"Group A","58":"2813981239821","59":"NH-501","60":"North Avenue","61":"22331","62":"Alexandria","63":"VA","64":"(742) 965-2838","101":"Yes","102":"Yes","103":"12/18/2034"}],
"150": "",
"154": "",
"159": "",
"164": "",
"165": "",
"309": [{"41":"Smith","42":"John","43":"S","44":"uncle","45":"(213) 123-2132","46":"(454) 324-5892","47":"(123) 456-7890","48":"Sa567","49":"AK","50":"11221","51":"Brooklyn","52":"NY"},{"41":"Smith2","42":"John","43":"2","44":"Uncle","45":"(723) 912-3912","46":"(217) 483-1912","47":"(475) 901-0371","48":"HY","49":"HU","50":"22331","51":"Alexandria","52":"VA","undefined":"Uncle"},{"41":"Harkness","42":"Jack","43":"Cap","44":"Son","45":"(989) 594-2019","46":"(917) 912-8129","47":"(371) 927-3912","48":"Thualsdj","49":"Nelujah","50":"22321","51":"Alexandria","52":"VA"}],
"310": "Smith",
"311": "John",
"313": "(111) 111-1111",
"314": "johnsmith@gmail.com",
"315": "sarathnagesh@live.com",
"318": "ABCD",
"320": "Dept1",
"500": [{"502":"Abc Pharnacy","504":"45-101","506":"South Avenue","508":"22331","510":"VA","512":"Alexandria","514":"(123) 213-2132","516":"(123) 213-2131"},{"502":"Gcd Pharmacy0","504":"45-10","506":"D-560","508":"22331","510":"VA","512":"Alexandria","514":"(731) 952-0520","516":"(841) 804-1930"}],
"1024": "["Do not Resuscitate","Healthcare Proxy"]",
"1051": "Yes"
}

53, 309, 500 are arrary object, don't put it inside ".." . I don't know what is "1024"

Robust
  • 2,415
  • 2
  • 22
  • 37
0

This is not a valid JSON, Use some json validator like http://jsonlint.com/ to validate it.

You have to add ',' to separate JSON Keys

E.g. 1: "Sarath",2: "nagesh

Now correct JSON Array by removing quotes from it:

correct JSON :

{"1": "Sarath","2": "nagesh","3": "R","4": "06/29/1991","5": "567-77-4576","6": "(999) 540-8778","7": "(655) 658-7369","8": "(398) 967-3996","9": "Male","10": "sarathnagesh@gmail.com","11": "89","12": "21 Wallstreet","13": "22331","14": "Alexandria","15": "VA","16": "ASD","17": "34","18": "Brooklyn Avenue","19": "22331","20": "Alexandria","21": "VA","22": "A+","23": "1","24": "m","25": "65","26": "cm","27": "56","28": "kg","29": "0","30": "gm","31": "Single","32": "","33": "","34": "","35": "","36": "","37": "","38": "","39": "","53": [    {"54": "XYZFincance","55": "Reese","56": "Son","57": "Group1","58": "jhg76fv7","59": "Dsad","60": "Dfdsf","61": "73301","62": "Austin","63": "TX","64": "(634)543-5345","101": "Yes","102": "Yes","103": "03/20/2015"    },    {"54": "ABCInsurance","55": "Ghv","56": "Son","57": "GroupA","58": "2813981239821","59": "NH-501","60": "NorthAvenue","61": "22331","62": "Alexandria","63": "VA","64": "(742)965-2838","101": "Yes","102": "Yes","103": "12/18/2034"    }],"150": "","154": "","159": "","164": "","165": "","309": [    {"41": "Smith","42": "John","43": "S","44": "uncle","45": "(213)123-2132","46": "(454)324-5892","47": "(123)456-7890","48": "Sa567","49": "AK","50": "11221","51": "Brooklyn","52": "NY"    },    {"41": "Smith2","42": "John","43": "2","44": "Uncle","45": "(723)912-3912","46": "(217)483-1912","47": "(475)901-0371","48": "HY","49": "HU","50": "22331","51": "Alexandria","52": "VA","undefined": "Uncle"    },    {"41": "Harkness","42": "Jack","43": "Cap","44": "Son","45": "(989)594-2019","46": "(917)912-8129","47": "(371)927-3912","48": "Thualsdj","49": "Nelujah","50": "22321","51": "Alexandria","52": "VA"    }],"310": "Smith","311": "John","313": "(111) 111-1111","314": "johnsmith@gmail.com","315": "sarathnagesh@live.com","318": "ABCD","320": "Dept1","500": [    {"502": "AbcPharnacy","504": "45-101","506": "SouthAvenue","508": "22331","510": "VA","512": "Alexandria","514": "(123)213-2132","516": "(123)213-2131"    },    {"502": "GcdPharmacy0","504": "45-10","506": "D-560","508": "22331","510": "VA","512": "Alexandria","514": "(731)952-0520","516": "(841)804-1930"    }],"1024": [    "DonotResuscitate",    "HealthcareProxy"],"1051": "Yes"}
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45