0

I have problem when trying to parse this JSON.

{
    "bakso-roso-n'deso__-6.19_106.77":
    {"Latitude":"-6.185488","Longitude":"106.77366","Distance":"90.89210930799594"},
    "print-point-duri-kepa__-6.19_106.77":
    {"Latitude":"-6.18599544813468","Longitude":"106.772603988647","Distance":"118.9849339548274"},
    "apartment-menara-kebun-jeruk__-6.19_106.78":
    {"Latitude":"-6.18530376709007","Longitude":"106.775222279179","Distance":"247.8816947606767"},
    "ranch-market---pesanggrahan__-6.19_106.77":
    {"Latitude":"-6.18761306472002","Longitude":"106.77343661177","Distance":"294.4255786871916"}
}

The problem is how I get "bakso-roso-n'deso__-6.19_106.77" as string. It doesn't have any tag.

Thank you.

Now regular way to handle Json is like this:

String jstring = "{\"menu\": {\"id\": \"file\", \"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
try
{
    jObject = new JSONObject(jstring);

How would I do that for this type of json? What should jstring be?

Basically I want to turn these json string into some dictionary.

1

3 Answers 3

3

plz check this string this is not a valid json String..

 String jstring = "{\"menu\": {\"id\": \"file\", \"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";

check the json is valid or not http://jsonlint.com/

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

Comments

2

Actually your "bakso-roso-n'deso__-6.19_106.77" is the tag (key ?) itself..

JSONObject jsonObject = new JSONObject(YOUR_RESPONSE_STRING);
Iterator<String> myIter = jsonObject.keys();
while(myIter.hasNext()){
  //here you can get all keys
}

Comments

0

I think you can get the iterator over keys using jObject.keys();

as per your code ....

String jstring = "{\"menu\": {\"id\": \"file\", \"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";


  try
      {
         jObject = new JSONObject(jstring);
         Map<String,String> map = new HashMap<String,String>();
         Iterator iter = jObject.keys();

         while(iter.hasNext()){
            String key = (String)iter.next();
            String value = menu.getString(key);
            map.put(key,value);
        }
    } catch(exception e){}

Parse JSON object with string and value only

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.