0
 {"1": {
         "StoreName": "イオン八千代緑が丘店",
         "StoreTel": "047-480-3660",
         "StoreAddress": "〒276-0049 千葉県八千代市緑ヶ丘2-1-3 2F",
         "WorkingTimeInNormalDay": "7:30 AM - 9:00PM",
         "WorkingTimeInWeekend": "9:00-22:00",
         "HaveKidProduct": "N"
     }}

i want to read this json:

JSONObject json ,jChild;
JSONArray jsonNames, jsonValues;
JSONArray jChildNames,jChildValues;
json = new JSONObject(resultString);

jsonNames = json.names();
jsonValues = json.toJSONArray(jsonNames);
for (int i = 0; i < jsonNames.length(); i++) {
jChild = jsonValues.getJSONObject(i);
jChildNames = jChild.names();
jChildValues = jChild.toJSONArray(jChildNames);

Log.i(getCallingPackage(), "No : " + jsonNames.getString(i));
                for (int j = 0; j < jChildNames.length(); j++){
                    Log.i(getCallingPackage(),jChildNames.getString(j) + " : " + jChildValues.getString(j).trim());

                }

}
}

There problems here is: When i parse this:

"WorkingTimeInNormalDay": "7:30 AM - 9:00PM", i have

jChildValues.getString(j) return "7" not "7:30 AM - 9:00PM"

i think may be ":" character is the root cause

How can i solve this problems?

Thanks

===========

Edited:

This is my mistake

Everything working like a champ

3
  • 1
    No, "7:30 AM - 9:00PM" shouldn't be causing any problem as its enclosed within double quotes Commented Feb 14, 2012 at 12:30
  • but if i dont have "WorkingTimeInNormalDay": "7:30 AM - 9:00PM", "WorkingTimeInWeekend": "9:00-22:00", everything is correct Commented Feb 14, 2012 at 12:56
  • I think it's an encoding problem. Print your json string. Commented Feb 14, 2012 at 13:12

4 Answers 4

1

Before parsing the json try to print the total json string so that you will know that "WorkingTimeInNormalDay": "7:30 AM - 9:00PM" is coming correctly or not. Because if you parse the json the WorkingTimeInNormalDay will give you 7:30 AM - 9:00PM

Sign up to request clarification or add additional context in comments.

2 Comments

"StoreName": "イオン八千代緑が丘店", "StoreTel": "047-480-3660", "StoreAddress": "〒276-0049 千葉県八千代市緑ヶ丘2-1-3 2F", i can parse correctly all of them
Yeah, the example JSON appears to be correctly formatted (by adding the missing {} required to make it a JSON object) - so it seems your actual input is invalid. If you are getting the JSON from an externally available source you should consider running it through an on-line validator, such as this.
1

well it worked here:

 String json = "{\"1\": [{\"StoreName\": \"イオン八千代緑が丘店\",\"StoreTel\": \"047-480-          3660\"," +
                "\"StoreAddress\": \"〒276-0049 千葉県八千代市緑ヶ丘2-1-3 2F\"," +
                "\"WorkingTimeInNormalDay\": \"7:30 AM - 9:00PM\"," +
                "\"WorkingTimeInWeekend\": \"9:00-22:00\"," +
                "\"HaveKidProduct\": \"N\"" +
                "}]}";


            try {
                JSONObject e = new JSONObject(json);
                JSONArray jArray = e.getJSONArray("1");
                 for(int i=0;i<jArray.length();i++){
                JSONObject obj = jArray.getJSONObject(i);
                 System.out.println(obj.getString("WorkingTimeInNormalDay"));
                }

                System.out.println(jArray.getString(0));
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }

1 Comment

I did not escape it if you look more carefully. I did not escape the : sign. I escaped the double quotes.
1

Take a look at the Google GSON library for Android. You can with ease add your own parsers for specific data types.

For instance:

public class DateDeserializer implements JsonDeserializer<Date>
{
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        String JSONDateToMilliseconds = "\\/(Date\\((.*?)(\\+.*)?\\))\\/";
        Pattern pattern = Pattern.compile(JSONDateToMilliseconds);
        Matcher matcher = pattern.matcher(json.getAsJsonPrimitive().getAsString());
        String result = matcher.replaceAll("$2");

        return new Date(new Long(result));
    }
}

1 Comment

but that will make you package that library with your apk instead of just using the native one.
0

Oh, there is no problems with ":" character.

There is a my big mistake.

Everything is work well

Sorry

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.