मुझे JSON मिला और मैंने मानों के आधार पर पार्स और सॉर्ट करने की कोशिश की। मैं इसे पार्स भी नहीं कर सका। नीचे मेरी JSON प्रतिक्रिया है।
[
{"scores":
{ "a":1.460211E-17,
"b":3.808661E-20,
"c":3.07329371E-14,
"d":1.02141569E-17,
"e":1,
"f":6.26432543E-12,
"g":3.75437664E-14,
"h":1.877707E-21
},
"rectangle":
{ "left":142,
"width":882,
"top":0,
"height":848
}
}
]
मैंने स्ट्रिंग को JSONObject के रूप में बदलने की कोशिश की, लेकिन एक त्रुटि यह JSONArray कहती है।
JSONObject reader = new JSONObject(json);
JSONArray jsonArray = reader.optJSONArray("scores");
जैसी त्रुटि हो रही है
org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject
इसलिए मैंने सोचा कि मूल्य पहले से ही एक JSON है। मैंने निम्नलिखित कोड की कोशिश की ...
JSONArray jsonArray = reader.optJSONArray("scores");
तथा
JSONArray cast = jsonResponse.getJSONArray("scores");
... लेकिन यह निम्न त्रुटि फेंकता है:
cannot resolve method optJSONArray()java.lang.String)
cannot resolve method getJSONArray()java.lang.String)
उत्तर:
उत्तर № 1 के लिए 1आपकी त्रुटि org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject
स्पष्ट रूप से कहता है कि आप पार्स करने की कोशिश कर रहे हैं JSONArray
ए के लिए JSONObject
यह करो
JSONArray reader = new JSONArray(json);
JSONObject jsonArray = reader.getJSONObject(0).optJSONObject("scores"); // replace 0 with your own index
जवाब के लिए 0 № 2
उदाहरण के लिए, यदि आप HTTP अनुरोध से अपना Json प्राप्त करते हैं, तो यह कोशिश करें:
HttpPost httppost = new HttpPost( "http://www.somepage.php" );
HttpParams mHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(mHttpParams, myConnectionTimeOut );
HttpConnectionParams.setSoTimeout(mHttpParams, myTimeOut);
HttpClient httpclient = new DefaultHttpClient( mHttpParams );
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader( entity.getContent() ));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = rd.readLine()) != null) {
sb.append(line );
}
JSONArray jsonArray = json.getJSONArray("scores");
for( int i = 0; i < jsonArray .length(); i++ ) {
/* Do what you want here*/
}
आशा करता हूँ की ये काम करेगा
जवाब के लिए 0 № 3
try {
JSONArray array= new JSONArray("your string");
JSONObject object= array.getJSONObject(0);
JSONObject
scores=object.getJSONObject("scores");
String [] subjects={"a","b","c","d","e","f","g","h"};
for(int i=0;i<subjects.length;i++){
Log.e("TAG",scores.getString(subjects[i]));
}
} catch (JSONException e) {
e.printStackTrace();
}