iOS JSON parser: what happens if two keys have the same name? - iphone

if the server returns:
{
"repositories":
{
"xmlns":{...}
"repository":{...}
"repository":{...}
}
}
then is it a valid JSON? I tested it in This JSON Parser and it works fine, but I wonder when I use some parser to parse it and get an NSDictionary, then for the key 'repository', how do I get the two repositories?
Thanks!

That JSON parser is not good then, the last value will overwrite the first one. Use JSONLint instead to validate your JSON, you will see the result.

Related

Is it possible to use JSONPath or XPath to get a query string?

I'm currently using Insomnia App to create some requests and one of my API responses I get a JSON with "redirect_uri" field and the value is actually a URL.
I need to get only the code parameter, so the JSONPath or XPath needs to return 8748d39f-1d4f-311f-92c6-4b279db1b317 in the following example:
{
"redirect_uri": "http://www.google.com?state=string&code=8748d39f-1d4f-311f-92c6-4b279db1b317"
}
Value to return: 8748d39f-1d4f-311f-92c6-4b279db1b317
Is it possible with JSONPath or XPath?
You can do it with this somewhat convoluted xpath expression:
substring-before(substring-after('{
"redirect_uri": "http://www.google.com?state=string&code=8748d39f-1d4f-311f-92c6-4b279db1b317"
}',
'code='),'"')

SmartGWT not able to parse data in the DataSource.transformResponse() method

I need some help please...
I am working with a GWT enabled web application. I am using the gwt-2.3.0 SDK.
I have a method that extends the DataSource class and uses the transformResponse method:
public class DeathRecordXmlDS extends DataSource {
protected void transformResponse(DSResponse response, DSRequest request, Object data){
super.transformResponse(response, request, data);
}
}
As I understand, the transformResponse() method should get control and at this point, I will have access to the data that is being provided to the Client side of my application. I am trying to work with the Object data parameter (the third parameter) that is passed in.
I am expecting an XML formatted string to be passed in. The XML will contain data (a count field) that I need to access and use.
I don't seem to be getting an XML string. Here's what I know...
I do see the XML data being passed to my webapp (the client). I can see this because I inspect the webpage that I am working with and I see the Response data. Here's an example of something that I expect to receive:
XML data from Query:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Collection numRecords="0">
<DeathRecords/>
</Collection>
The above XML is valid (I checked it in a Validator). This is a case where there was no data (No Death Records) being returned to my application. The numRecords XML attribute is set to "0". Of course, If I do have records returned the numRecords will contain the number of records and I'll get that same number of DeathRecord nodes.
I am not getting the above data (or, I don't know how to work with it) in the transformResponse() method.
Here's what I've done to try to figure this out...
The Object data parameter... it is a JavaScriptObject. I know this because I did a .getClass().getName() on it:
DeathRecordXmlDS::transformResponse() data.getClass().getName(): com.google.gwt.core.client.JavaScriptObject$
Then, to try to work with it, I converted it to a String:
com.google.gwt.core.client.JavaScriptObject dataJS = (com.google.gwt.core.client.JavaScriptObject)data;
System.out.println("DeathRecordXmlDS::transformResponse() data as a JavaScriptObject: "+dataJS.toString());
The contents of 'data' formatted as a String look like:
DeathRecordXmlDS::transformResponse() data as a JavaScriptObject: [XMLDoc <Collection>]
So, it looks like I have something that has to do with my 'Collection' node, but not a String of XML data that I can parse and get to my numRecords attribute.
What do I need to do to gain access to the XML in the transformResponse() method?
Thanks!
I think your data object is already translated to a javascript collection.
Maybe you could use the utility class XMLTools to retrieve your numRecords information:
Integer numRecords = Integer.parseInt(XMLTools.selectString(data, "Collection/#numRecords"));
After working on this for an additional period of time I was able to read the XML data that I am working with. I used the following piece of code:
try{
JsArray<JavaScriptObject> nodes = ((JavaScriptObject) XMLTools.selectNodes(data, "/Collection/#numRecords")).cast();
for (int i = 0; i < nodes.length(); i++) {
com.google.gwt.dom.client.Element element = (com.google.gwt.dom.client.Element) nodes.get(i);
numRecords = element.getNodeValue();
}
} catch(Exception e){
// If Parsing fails, capture the exception
System.out.println("DeathRecordXmlDS::transformResponse() Not able to parse the XML");
}
I think the first step to solving this was understanding that the parameter 'data' of type Object was really a JavaScriptObject. I learned this after looking at the .getClass() and .getName(). This helped me understand what I was working with:
System.out.println("DeathRecordXmlDS::transformResponse() data.getClass().getName(): "+data.getClass().getName());
Once I knew it was a JavaScriptObject, I was able to do a little more focused of a Google search for what I was trying to accomplish. I was a little surprised that the XMLTools.selectNodes() function worked the way it did, but the end result is that I was able to read the numRecords attribute.
Thanks for the suggestion!

How to map different json with RestKit?

Im new to Restkit and I want to use it for my iOS project.
I ran into a problem, because I get different JSON back from the server
and I don't know how to configure my mapping with RestKit.
Here are the two types of JSON messages I can receive from the server.
First json message:
{"result":"not found"}
Second json message:
{ "OPDRACHT":
{ "streetnaam":"value",
"postalcode":"value",
"city":"value"
}
}
I've been reading in the RestKit documentation about mapping without KVC and Dynamic Object Mapping but I can't figure out how to configure the mapping for RestKit.
Anyone an idea how to configure the mapping with RestKit in this situation?
I fixed my mapping problem today.
I changed my json output from the server in a more object structure. Now
RestKit works great with my json.

Creating Objects from JSON

I would like to be able to create an NSArray or NSDictionary with a JSON string that I receive from a web service. I am using the json framework to do this. The response string will look something along these lines:
{count:2,
data:[{"ID":8,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.00",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:25:56.0"
},
{"ID":9,
"Title":"Test Title",
"Author":"Test Author",
"Price":"18.000000000000",
"Edition":"1st",
"Condition":"Good",
"UploadDate":"2012-07-28 07:27:06.0"
}
]
}
My question is, what is the most efficient way to grab all of the data from the 'data' array and use it to create either an NSArray or NSDictionary? Any help would be much appreciated
I use RestKit for JSON parsing. It has all you need to fetch the json from a server, translate it into a dictionary, and from there into an object that you can work with. That might be more than you dared to wish for, but it works like a charm. Check out https://github.com/RestKit/RestKit for more info.
Starting from iOS5 a json parser has been integrated as a SDK native component see the following link http://www.raywenderlich.com/5492/working-with-json-in-ios-5.
You will create a NSDictionary with 2 keys "count" and "data" and then you will get the NSArray using objectForKey with the "data" key.

How we can handle dynamic web service in iPhone?

I am learning some tricky development in iPhone and during my experiments I found out that usually we used localized web-service in which all parameter are fixed(Keyword). If my web service will change some fields in the response than how can we handle in iPhone. Please help me. If Anybody have any good idea.
For Example,
Webservice Response1:
[    {
      "Number":"A12 hrb",
      "List":[
         {
            "Type":"Works",
            "Display":{
               "dop":45,
               "dopper":56
            },
            "OAST":"10-01-2012",
            "OAET":"07-04-2012",
            "Cause":"define",
            "Impact":"Queue",
            "Description":"Take a Break.",
            "LName":"Lunetten To Lunetten",
            "Number":"A12 hrb",
         }
      ]    },   ]
Webservice Response2:
[    {
      "Number":"A12 hrb",
"Number2":"A13 brs",
      "List":[
         {
            "Type":"Works",
            "Display":{
               "dop":45,
               "dopper":56
"picker":90
            },
            "OAST":"10-01-2012",
"MAET":"07-04-2012",
            "OAET":"07-04-2012",
            "Cause":"define",
            "Impact":"Queue",
            "Description":"Take a Break.",
            "LName":"Lunetten To Lunetten",
            "Number":"A12 hrb",
         }
      ]    },   ]
You can do this
Parse the response.If response is JSON then definitely you will get a dictionary just keep a reference of it.
you can get all the keys in dictionary by calling following method
(NSArray *)allKeys
now enumerate above array and access the values respective to each key and do whatever you want
But you should know the meaning/purpose of dynamic keys. If you don't no meaning/purpose of keys these steps may not help you... best of luck.
For this type of case you can get the dictionary and in dictionary you
can get the value of which tag you want means you just need root node
and store root node all the data in dictionary and handle that
dictionary for the further use..
I don't think it will be possible to parse it completely. Atleast you should know which keys are going to be there. e.g. response has Number, Number2 & List as keys. It's ok if some responses do not contain one/some of the keys.
On the other hand, if knowing all the keys in advance is at all not possible, then webservice should have mechanism to convey the keys used in response.
e.g. [ {
"dynamic_keys": "Number2",
"Number":"A12 hrb",
"Number2":"A13 brs",
"List":[
{
"Type":"Works",
"Display":{
"dop":45,
"dopper":56
"picker":90
},
"OAST":"10-01-2012",
"MAET":"07-04-2012",
"OAET":"07-04-2012",
"Cause":"define",
"Impact":"Queue",
"Description":"Take a Break.",
"LName":"Lunetten To Lunetten",
"Number":"A12 hrb",
}
] }, ]
You can read the value of "dynamic_keys" and then using that value you can read value of actual dynamic key.
edit: as mentioned by ssteinberg you can use some framework like JSONKit to parse actual JSON.
See this as well: How to parse JSON having dynamic key node