0

I am trying to port a piece of Code from Java to C# and I am stuck in JSon parsing. Have a look at the following Java Code

        mJsonObject = new JSONObject(str);
        Iterator<String> keys=mJsonObject.keys();
        while(keys.hasNext()){

            String key=keys.next();
            String value=mJsonObject.getString(key);

            mAdData.add(new AdData(key, new JSONObject(value)));


        }

I had a string which has verified Json format and I passed it to JSONObject and every thing was finely working in Java, but now in C# Unity I am not able to port it successful. I am using LitJson to perform this task and I have no idea how this works. I am badly stuck please help. Thanks

2
  • Do you get an exception, what is the issue, please provide some detail Commented Dec 22, 2014 at 10:40
  • The above mentioned code is Java code I need its conversion to C#. I have tried some but failed Here is the code JsonData mJsonObject = JsonMapper.ToObject (dataObj); IEnumerator keys = (IEnumerator)mJsonObject.Keys; Error on second line is System.InvalidCastException: Cannot cast from source type to destination type. Commented Dec 22, 2014 at 10:42

1 Answer 1

1

The keys method of the JSONObject class returns an ICollection<string>. You can iterate an ICollection like this. So I would change your while loop into a foreach, like this:

foreach (string key in keys) {
    //whatever
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot its working. Now the next step is to get the value against the key. How can I do that? Please let me know.
Please, take a look here: stackoverflow.com/questions/19974763/…. If an answer solves your problem, then please consider accepting it to help future visitors having the same kind of problem.
the above method does not work for me unfortunately. If my problem is solved I will definitely accept the answer.

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.