[英]How can I convert String to JsonObject?
{
"error":null,
"countries":[
{
"id":1,
"description":"Slovensko",
"locale":"sk_SK",
"zipRegexp":"/^\\d{3}\\ ?\\d{2}$/"
},
{
"id":2,
"description":"Česká republika",
"locale":"cs_CZ",
"zipRegexp":null
},
{
"id":3,
"description":"Afganistan",
"locale":"en_EN",
"zipRegexp":null
}
]
}
and so on so on, now I need to convert it to JsonObject on Android. 依此类推,现在我需要将其转换为Android上的JsonObject。 But I cannot convert it to String because of double commas .
但是由于双逗号,我无法将其转换为String 。 How can it be done?
如何做呢? I need somethink like this:
我需要这样的想法:
JsonObjet.JsonFromString('{"error":null,"countries":[{"id":1,"description":"Slovensko","locale":"sk_SK","zipRegexp":"/^\\d{3}\\ ?\\d{2}$/"},{"id":2,"description":"Česká republika","locale":"cs_CZ","zipRegexp":null}}');
this is not the answer: 这不是答案:
JSONObject getJSON = new JSONObject("{"error":null,"countries":[{"id":1,"description":"Slovensko","locale":"sk_SK","zipRegexp":"/^\\d{3}\\ ?\\d{2}$/"},{"id":2,"description":"Česká republika","locale":"cs_CZ","zipRegexp":null}}");
because of double commas. 因为双逗号。
thanks 谢谢
JSONObject getJSON = new JSONObject("Your String");
String yourString = "{"+"\"error\""+":"+"\"no error\""+"}";
System.out.println(yourString );
Then yourString
will be {"error":"no error"}
. 然后
yourString
将是{"error":"no error"}
。 Implement the same way for the String you have. 对您拥有的String实施相同的方法。 Then convert the above
yourString
to JSONObject as 然后将上面的
yourString
转换为JSONObject作为
JSONObject getJSON = new JSONObject(yourString);
How do you get the JSon??? 您如何获得JSon ??? If you are getting it from a HttpClient then get it as a InputStream and use the bufferedReader to convert it into a String buffer.
如果您是从HttpClient获得的,则将其作为InputStream获得,并使用bufferedReader将其转换为String缓冲区。
JSONObject jsonObject = new JSONObject(res);
Try this : 尝试这个 :
InputStream mIs = null;
String result = "";
JSONObject jObjectLogin = null;
HttpClient httpclient = new DefaultHttpClient();
String urlWithNoSpace= url.replace(" ", "%20");
HttpGet httpget = new HttpGet(urlWithNoSpace);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
mIs = entity.getContent();
String result = "";
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(mIs,"UTF-8"),8);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = bufferReader.readLine()) != null) {
//if require change or edit this condition.
if(line.trim().equals("\n"))
continue;
stringBuilder.append(line + "\n");
}
mIs.close();
result=stringBuilder.toString();
Now as according to string format(which is in Json format) you can fetch array and objects from it. 现在,根据字符串格式(Json格式),您可以从中获取数组和对象。
JSONObject jsonObject=result ;
JSONArray jsonArray=jsonObject.getJSONArray("countries");
now put loop and find values fron Array. 现在放入循环并从数组中查找值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.