0
{
   "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. But I cannot convert it to String because of double commas. 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

4

4 回答 4

2

你怎么得到这个JSON???如果您从 HttpClient 获取它,则将其作为 InputStream 获取并使用 bufferedReader 将其转换为 String 缓冲区。

于 2013-06-26T07:07:38.247 回答
2
JSONObject getJSON = new JSONObject("Your String");

编辑:

String yourString = "{"+"\"error\""+":"+"\"no error\""+"}";
System.out.println(yourString );

然后yourString{"error":"no error"}。为您拥有的 String 实现相同的方式。然后将上述转换yourString为 JSONObject 作为

JSONObject getJSON = new JSONObject(yourString);
于 2013-06-26T06:17:36.157 回答
1

尝试这个 :

        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();

现在根据字符串格式(Json 格式),您可以从中获取数组和对象。

     JSONObject jsonObject=result ;
     JSONArray jsonArray=jsonObject.getJSONArray("countries");

现在放循环并从数组中查找值。

于 2013-06-26T06:25:18.850 回答
1
JSONObject jsonObject = new JSONObject(res);
于 2013-06-26T06:16:23.597 回答