Shubh
Shubh

Reputation: 6731

get value from string

I have a string from json data as below:-

"Order Number : 123i the prodfuct is hard to found Title Name superman Product Code :ABC01 Number Of Instances :1 Duration : 10.91 Size : 0.0 Product Cost : $10.43 Product Code :HELLO123 Number Of Instances :1 Duration : 0.91 Size : 0.0 Product Cost : $0.0 "

I want to separate the values into an array!

Need help, i am new to jquery, and tried other option's to!

Upvotes: 0

Views: 124

Answers (1)

techfoobar
techfoobar

Reputation: 66693

First you need to format your json string correctly using commas to separate key value pairs, making sure your key names don't have spaces in them and using double quotes to wrap non-numeric values. For ex:

{
  Order_Number : "123i the prodfuct is hard to found Title Name superman", 
  Products: [
    { 
      Product_Code : "ABC01",
      Number_Of_Instances : 1,
      Duration: 10.91,
      Size : 0.0,
      Product_Cost : "$10.43"
    },
    { 
      Product_Code : "ABC02",
      Number_Of_Instances : 2,
      Duration: 5.91,
      Size : 0.0,
      Product_Cost : "$5.43"
    }
  ]
}

Once you have this data in a json string, you can parse it like:

var data = JSON.parse(jsonString);

var orderNumber = data.Order_Number; // this is your order number

// this is how you iterate through the products in the order
for(product in data.Products) {
  var code = product.Product_Code;
  var instances = product.Number_Of_Instances;
  // and so on..
}

Upvotes: 2

Related Questions