user6112961
user6112961

Reputation:

Json Parse android

Hi guys i got a json string like

{
"Successful": true,
"Value": "{\"MesajTipi\":1,\"Mesaj\":\"{\\\"Yeni\\\":\\\"Hayır\\\",\\\"Oid\\\":\\\"3d9b81c9-b7b3-4316-8a73-ad4d54ee02a8\\\",\\\"OzelKod\\\":\\\"\\\",\\\"Adet\\\":1,\\\"ProblemTanimi\\\":\\\"999\\\",\\\"HataTespitYeri\\\":\\\"Montajda\\\",\\\"Tekrar\\\":\\\"Evet\\\",\\\"ResmiBildirimNo\\\":\\\"\\\",\\\"Malzeme\\\":\\\"5475ffdb-0bc0-49cb-9186-429c60dbf91b\\\",\\\"HataKodu\\\":\\\"c30df623-496b-4a62-ba16-493bd435ca33\\\",\\\"Tarih\\\":\\\"2016-04-16 10:34:00\\\",\\\"KayitNo\\\":\\\"1600010.2\\\"}\"}"
}

I need to get "Oid" value from this string. I tried to get it with

            Gson gson = new Gson();
            JsonParser parse = new JsonParser();
            JsonObject jsonobj = (JsonObject) parse.parse(snc);
            String stroid = jsonobj.get("Oid").toString();

But it gives null referance exception ? Any idea how can i get the only Oid value ?

Edit

I already tried How to parse JSON in Java but still no succes

What i tried from this page :

String pageName = jsonObj.getJSONObject("Value").getJSONObject("Mesaj").getString("Oid");

Upvotes: 0

Views: 66

Answers (2)

amir
amir

Reputation: 167

lets say this is your JSON

{
"success":"ok",
"test" : [{"id" : "1" ,"name" : "first"}]
,"RPTF":
    [{"cats":[{"id" : "1" ,"name" : "first"},
              {"id" : "1" ,"name" : "first"},
              {"id" : "2" ,"name" : "test"} ]


    ,"pics" : [{"id" : "12" ,"value" : "description" ,"src" : "http://citygram.ir" ,"visit" : "3" },
               {"id" : "10" ,"value" : "description" ,"src" : "http://citygram.ir" ,"visit" : "3" } ]
  }]
}

method number one (if you want to access "success")

    public String jsonOne(String json, String target) {

    String result;
    try {

        JSONObject jo = new JSONObject(json);
        result = jo.getString(target);

    } catch (JSONException e) {

        return "";
    }
    return result;
}

you can simply call this method like this

jsonOne(String json, "success");

method number two (in case you want to access "id"s inside the "test")

public String[] jsonTwo(String json, String target0, String target1) {

    String[] result;
    result = new String[1];

    try {
        JSONObject jo = new JSONObject(json);
        JSONArray ja = jo.getJSONArray(target0);

        result = new String[ja.length()];
        for (int i = 0; i < ja.length(); i++) {
            JSONObject jojo = ja.getJSONObject(i);
            result[i] = jojo.getString(target1);
        }

    } catch (JSONException e) {
    }
    return result;
}

you get an array String on call :

jsonTwo(String json, "test", "id");

method number three (let's say you want to access "value"s in side "pics")

public String[] jsonThree(String json, String target0, String target1,String target2) {

    String[] result;
    result = new String[1];

    try {
        JSONObject jo = new JSONObject(json);
        JSONArray ja = jo.getJSONArray(target0);
        JSONObject jojo=ja.getJSONObject(0);
        JSONArray jaja=jojo.getJSONArray(target1);

        result = new String[jaja.length()];
        for (int i = 0; i < ja.length(); i++) {
            JSONObject jojojo = jaja.getJSONObject(i);
            result[i] = jojojo.getString(target2);
        }
    } catch (JSONException e) {
        toast("Wrong");
    }

    return result;
}

on call :

jsonThree(String json, "RPTF", "pics", "value");

you can write more methods like this and just call them .

Upvotes: 1

Pooya
Pooya

Reputation: 6136

The problem is because of extra " " in the "Value" section of JSON, so it will be treated as String not an object. The better solution is to seek the source of the JSON to see why they added extra " " and clear that but if you don't have access the source I propose the following:

Replace:

String stroid = jsonobj.get("Oid").toString();

with:

String valueString = jsonobj.getString("Value");
jsonobj = new JSONObject(valueString);
String mesagString = jsonobj.getString("Mesaj");
jsonobj = new JSONObject(mesagString);
String stroid = jsonobj.getInt("Oid").toString();

Upvotes: 0

Related Questions